﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration.Provider;
using Barbar.MapReport.DataSource.Resources;
using Barbar.MapReport.Common;

namespace Barbar.MapReport.DataSource {
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface")]
  public class RegionGeometryProviderCollection : ProviderCollection {
    public override void Add(ProviderBase provider) {
      if (provider == null)
        throw new ArgumentNullException("provider");

      if (!(provider is RegionGeometryProvider))
        throw new ArgumentException(Translator.Error_ProviderMustImplementRegionGeometryProvider, "provider");

      base.Add(provider);
    }
        
    public Polygon GetRegionGeometry(string regionId, ZoomLevel zoomLevel) {
      if (String.IsNullOrEmpty(regionId))
        throw new ArgumentNullException("regionId");

      Polygon result = RegionGeometryCache.GetRegionGeometry(regionId, zoomLevel);
      if (result != null)
        return result;

      foreach (RegionGeometryProvider provider in this) {
        if (provider.ContainsRegionGeometry(regionId, zoomLevel)) {
          result = provider.GetRegionGeometry(regionId, zoomLevel);
          RegionGeometryCache.SetRegionGeometry(regionId, zoomLevel, result);
          return result;
        }
      }

      return null;
    }
  }
}
